/*
 * Copyright (c) 2007 The Chai Team
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package com.google.projects.chai.data;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Used as a testing ground for some random stuff before we get things really
 * started.
 *
 * @author dap
 */
public class Playground {
        
        SessionFactory sessionFactory;
        
        Playground(SessionFactory fact) {
                sessionFactory = fact;
        }
        
        void createUser() {
                Session session = sessionFactory.getCurrentSession();
                session.beginTransaction();
                
                User user = new User();
                user.setDisplayName("Dave");
                user.setUsername("dap");
                user.setLocation("San Francisco, CA");
                
                session.save(user);
                session.getTransaction().commit();
        }
        
        void createUserWithGroup() {
                Session session = sessionFactory.getCurrentSession();
                session.beginTransaction();
                
                User c = new User();
                c.setDisplayName("Bill");
                c.setUsername("wdp");
                
                User s = new User();
                s.setDisplayName("Stephanie");
                s.setUsername("sgreer");
                
                session.save(c);
                session.save(s);
                
                UserGroup cg = new UserGroup();
                cg.setName("Bill's contacts");
                c.getGroups().add(cg);
                cg.setOwner(c);
                cg.getMembers().add(s);
                session.save(cg);
                
                session.getTransaction().commit();
        }
        
        void done() {
                sessionFactory.close();
        }
        
        public static void main(String []args) {
                Playground pg = new Playground(HibernateUtil.getSessionFactory());
                pg.createUserWithGroup();
                pg.done();
        }
}
